<?php
namespace App\Api\Controllers\Zgjl;


use App\Api\Controllers\Cmbc\BaseController;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OauthController extends BaseController
{

    //授权
    public function oauth(Request $request)
    {
        $sub_info = $request->get('state');
        return redirect(url('api/zgjl/weixin/callback?sub_info=' . $sub_info . '&wx_AppId=111' . '&wx_Secret=111'. ''));
    }


    public function callback(Request $request)
    {
        $sub_info = $request->get('sub_info');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => 11,
            'merchant_id' => $merchant_id
        ];
        $data = base64_encode(json_encode((array)$data));

        return redirect('/api/zgjl/weixin/pay_view?data=' . $data);
    }
    
     public function unionpay(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_pid=0;
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => 11,
            'merchant_id' => $sub_info_arr['merchant_id']
        ];

        return view('zgjl.unionpay', compact('data'));
    }


    //支付显示页面
    public function pay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);
        $data['is_goods'] = Store::where('store_id', $data['store_id'])->value('is_goods');
        $data['store_btn'] = json_decode(json_encode(DB::table('store_goods_btn')->where('store_id', $data['store_id'])->first()), true);

        return view('zgjl.weixin', compact('data'));
    }





}
